/*
 * Created on 2004-01-06
 */
package org.opencube.oms;

import java.util.Date;

import org.opencube.util.Constants;

/**
 * The representation of the oms node.
 * 
 * Besides attributes common for nodes of each
 * type as <code>id</code>, <code>name</code>, <code>creatingDate</code>, <code>modifyingDate</code>,
 * <code>creater</code> and <code>modifier</code>, it provides the modification functionality. 
 * 
 * 
 * @author <a href="mailto:maciek@fingo.pl">FINGO - Maciej Mroczko</a>
 */
public class OMSNode
{
	/**
	 * The node state constant: normal = 0
	 */
	public static final int STATE_NORMAL = 0;

	/**
	 * The node state constant: new = 1
	 */
	public static final int STATE_NEW = 1;

	/**
	 * The node state constant: updated = 2
	 */
	public static final int STATE_UPDATED = 2;

	/**
	 * The node state constant. removed = 3
	 */
	public static final int STATE_REMOVED = 3;
	
	/**
	 * The node state constant. STATE_KEYUPDATED = 4
	 */
	public static final int STATE_KEYUPDATED = 4;
	
	/**
	 * The node id
	 */
	protected String id = Constants.STR_ZERO;
	
	/**
	 * The node name
	 */
	protected String name = null;
	
	/**
	 * Creation date of the node
	 */
	protected Date creatingDate = null;
	 
	/**
	 * Modification modi of the node
	 */
	protected Date modifyingDate = null;
	
	/**
	 * The node creater id
	 */
	protected String creater = Constants.STR_ZERO;

	/**
	 * The node modifier id
	 */
	protected String modifier = Constants.STR_ZERO;
	
	/* State of the node */
	private int state = STATE_NORMAL;

	/**
	 * The full constructor
	 * 
	 * @param id - the id of the node
	 * @param name - name
	 * @param creatingDate - creation date
	 * @param creater - node creater id
	 * @param modifyingDate - modification date
	 * @param modifier - node modifier id 
	 */
	protected OMSNode( String id, String name, Date creatingDate, String creater, Date modifyingDate, String modifier )
	{
		this.id = id;
		this.name = name;
		this.creatingDate = creatingDate;
		this.creater = creater;
		this.modifyingDate = modifyingDate;
		this.modifier = modifier;
	}

	/**
	 * Sets the node state
	 * 
	 * @param state int - new state for the node
	 */
	protected void setState( int state )
	{
		if ( ( state == STATE_UPDATED && this.state == STATE_NORMAL )
			 || ( state == STATE_REMOVED && this.state != STATE_NEW )
			 || state == STATE_NEW 
			 || state == STATE_NORMAL 
			 || state == STATE_KEYUPDATED	 
		)
		{
			this.state = state;
		}
	}

	/**
	 * Returns the actual state of this node
	 * 
	 * @return int - the node actual state 
	 */
	public int getState()
	{
		return this.state;
	}

	/**
	 * Returns the id of the node creater
	 * 
	 * @return String - id of the node creater
	 */
	public String getCreater()
	{
		return this.creater;
	}

	/**
	 * Returns the node creation date
	 * 
	 * @return Date - the node creation date
	 */
	public Date getCreatingDate()
	{
		return this.creatingDate;
	}

	/**
	 * Returns the node id
	 * 
	 * @return String - the node id
	 */
	public String getId()
	{
		return this.id;
	}

	/**
	 * Returns the node modification date
	 * 
	 * @return Date - modification date
	 */
	public Date getModifyingDate()
	{
		return this.modifyingDate;
	}

	/**
	 * Returns the id of the node modifier
	 * 
	 * @return String - id of the node modifier
	 */
	public String getModifier()
	{
		return this.modifier;
	}

	/**
	 * Returns name of this node
	 * 
	 * @return String - the node name
	 */
	public String getName()
	{
		return this.name;
	}

	/*
	 * (non-Javadoc)
	 * @see java.lang.Object#toString()
	 */
	public String toString()
	{
		return "<name=\"" + this.name + "\" id=\"" + this.id + "\" state=" + this.state + "\"/>";
	}
	
	/**
	 * Sets the name of this node and changes its state
	 * 
	 * @param name - the new name for this node
	 */
	protected void update( String name ) throws OMSStructureException
	{
		if( !( name == null && this.name == null ) && !( name != null && name.equals( this.name ) )  )
		{
			this.name = name;
			if( !isNew() )
			{
				setState( STATE_UPDATED );
			}
		}
	}
	
	/**
	 * Marks this node as deleted  - by changing its state 
	 */
	protected void delete() throws OMSStructureException
	{
		setState( STATE_REMOVED );
	}
	
	/**
	 * Tests the node state, if it is updated.
	 * 
	 * @return boolean - <code>true</code>, if the node is updated 
	 */
	public boolean isUpdated()
	{
		return this.state == STATE_UPDATED;
	}
	
	public boolean isRemoved()
	{
		return this.state == STATE_REMOVED;
	}
	
	public boolean isNew()
	{
		return this.state == STATE_NEW;
	}
	
	/* Sets the id for the node */
	public void setId( String id )
	{
		this.id = id;
	}
}
